package net.common.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.common.service.PageBean;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

/**
 * action基类
 * 
 * @author 孙树林
 * 
 */
public class GeneralAction extends ActionSupport {

	private static final long serialVersionUID = -4477671182461052813L;

	protected PageBean pageBean = new PageBean();

	/**
	 * 输出json字符串
	 * 
	 * @param str
	 * @throws Exception
	 */
	protected void json(Object o) throws Exception {
		HttpServletResponse response = getResponse();
		response.setContentType("text/xml");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/html; charset=utf-8");
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		out.write(gson.toJson(o));
		out.close();
	}
	
	protected void out(String str) throws IOException {
		HttpServletResponse response = getResponse();
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(str);
		out.close();
	}
	
	/**
	 * response
	 * 
	 * @return
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
}
